﻿using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Commons;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Design.WebUserControls.Frame
{
    public partial class ContractFrame : UserControl
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.LoadParticipation();
            }
        }

        private void LoadParticipation()
        {
            DropDownListParticipation.DataSource = DocumentUtilities.Participation;
            DropDownListParticipation.DataBind();
        }

        /// <summary>
        /// Gets the contract details.
        /// </summary>
        /// <returns></returns>
        [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
        public ServiceContract GetServiceContractDetails()
        {
            var contract = new ServiceContract();
            string contractOutCome = string.Join("-",
                                                 CheckBoxListContractOutcome.Items.Cast<ListItem>().Where(
                                                     c => c.Selected).Select(c => c.Value));
            string contractObligation = string.Join("-",
                                                    CheckBoxListContractObligation.Items.Cast<ListItem>().Where(
                                                        c => c.Selected).Select(c => c.Value));

            contract.ContractOutcome = contractOutCome;
            contract.ContractObligation = contractObligation;
            contract.Participation = DropDownListParticipation.SelectedValue;

            return contract;
        }

        /// <summary>
        /// Loads the service contract details.
        /// </summary>
        /// <param name="serviceContract">The service contract.</param>
        public void LoadServiceContractDetails(ServiceContract serviceContract)
        {
            if (serviceContract == null)
            {
                return;
            }

            CheckBoxListContractOutcome.DataSource = ServiceBO.GetServiceReferenceDataByCategoryId(14);
            CheckBoxListContractObligation.DataSource = ServiceBO.GetServiceReferenceDataByCategoryId(15);

            CheckBoxListContractOutcome.DataBind();
            CheckBoxListContractObligation.DataBind();

            DropDownListParticipation.SelectedValue = serviceContract.Participation;
            SetSelectedItemsInCheckBoxList(serviceContract.ContractOutcome, CheckBoxListContractOutcome);
            SetSelectedItemsInCheckBoxList(serviceContract.ContractObligation, CheckBoxListContractObligation);
        }

        /// <summary>
        /// Sets the selected items in check box list.
        /// </summary>
        /// <param name="selectedIds">The selected ids.</param>
        /// <param name="checkBoxList">The check box list.</param>
        private static void SetSelectedItemsInCheckBoxList(string selectedIds, CheckBoxList checkBoxList)
        {
            string[] selectedIdsArr = (selectedIds ?? string.Empty).Split('-');
            foreach (ListItem item in checkBoxList.Items)
            {
                if (selectedIdsArr.Contains(item.Value))
                {
                    item.Selected = true;
                }
            }
        }
    }
}